package com.taoym.voucher.xcx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taoym.voucher.common.enums.ActivityOrderStatusEnum;
import com.taoym.voucher.common.enums.ActivityOrderTypeEnum;
import com.taoym.voucher.common.enums.DateConditionEnum;
import com.taoym.voucher.common.enums.OrderBuyTypeEnum;
import com.taoym.voucher.common.utils.ThreadLocalUtils;
import com.taoym.voucher.xcx.domain.XcxActivityOrder;
import com.taoym.voucher.xcx.entity.dto.XcxGetActivityOrdersDTO;
import com.taoym.voucher.xcx.entity.vo.XcxGetOrderVO;
import com.taoym.voucher.xcx.mapper.XcxActivityOrderMapper;
import com.taoym.voucher.xcx.service.XcxActivityOrderService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @coder taoym
 * @datetime 2021/6/11 15:50
 * @desc 活动订单服务接口实现类
 */
@Service
public class XcxActivityOrderServiceImpl extends ServiceImpl<XcxActivityOrderMapper, XcxActivityOrder> implements XcxActivityOrderService {

    @Resource
    private XcxActivityOrderMapper xcxActivityOrderMapper;

    @Override
    public List<XcxGetOrderVO> getOrderList(XcxGetActivityOrdersDTO data) {

        String uid = ThreadLocalUtils.get().getUid();
        Map<String, LocalDateTime> dateTimeMap = DateConditionEnum.getDateLocalDate(data.getDateType());
        List<XcxActivityOrder> orders = null;
        List<XcxGetOrderVO> xcxGetOrderVOS = null;
        if (data.getType().equals(OrderBuyTypeEnum.MYSELF.getCode())) {
            orders = xcxActivityOrderMapper.getMyselfActivityOrderList(uid, data.getStatus(),
                    dateTimeMap.get("start"), dateTimeMap.get("end"),
                    (data.getPage() - 1) * data.getSize(), data.getSize());
            if (CollectionUtils.isEmpty(orders)) {
                return null;
            }
            xcxGetOrderVOS = new ArrayList<>();
            for (XcxActivityOrder order : orders) {
                XcxGetOrderVO xcxGetOrderVO = new XcxGetOrderVO();
                xcxGetOrderVO.setOrderTitle(order.getOrderTitle());
                xcxGetOrderVO.setActName(order.getActName());
                xcxGetOrderVO.setActivityColor(ActivityOrderTypeEnum.getColor(order.getActId()));
                xcxGetOrderVO.setChiefFee(order.getOrderUserFee());
                xcxGetOrderVO.setPayPrice(order.getPayPrice());
                xcxGetOrderVO.setStatusDesc(order.getStatusDesc());
                xcxGetOrderVO.setStatusColor(ActivityOrderStatusEnum.getColor(order.getStatus()));
                xcxGetOrderVO.setPayTime(order.getPayTime());
                xcxGetOrderVOS.add(xcxGetOrderVO);
            }
        } else {
            orders = xcxActivityOrderMapper.getTeamActivityOrderList(uid, data.getStatus(),
                    dateTimeMap.get("start"), dateTimeMap.get("end"),
                    (data.getPage() - 1) * data.getSize(), data.getSize());
            if (CollectionUtils.isEmpty(orders)) {
                return null;
            }
            xcxGetOrderVOS = new ArrayList<>();
            for (XcxActivityOrder order : orders) {
                XcxGetOrderVO xcxGetOrderVO = new XcxGetOrderVO();
                xcxGetOrderVO.setOrderTitle(order.getOrderTitle());
                xcxGetOrderVO.setActName(order.getActName());
                xcxGetOrderVO.setActivityColor(ActivityOrderTypeEnum.getColor(order.getActId()));
                xcxGetOrderVO.setChiefFee(order.getOrderUserFee());
                xcxGetOrderVO.setPayPrice(order.getPayPrice());
                xcxGetOrderVO.setStatusDesc(order.getStatusDesc());
                xcxGetOrderVO.setStatusColor(ActivityOrderStatusEnum.getColor(order.getStatus()));
                xcxGetOrderVO.setPayTime(order.getPayTime());
                xcxGetOrderVOS.add(xcxGetOrderVO);
            }
        }
        return xcxGetOrderVOS;
    }
}
